package com.alibaba.datax.core.transport.transformer;

import com.alibaba.datax.common.element.Column;
import com.alibaba.datax.common.element.Record;
import com.alibaba.datax.common.element.StringColumn;
import com.alibaba.datax.common.exception.DataXException;
import com.alibaba.datax.transformer.Transformer;

import java.util.Arrays;

/**
 *
 * {
 *     "name": "dx_dataversion",
 *     "parameter": {
 *         "columnIndex": 5,
 *         "paras": [
 *             "版本号"
 *         ]
 *     }
 * }
 *
 * @author 张陶
 *
 */
public class DataVersionTransformer extends Transformer {

    public DataVersionTransformer() {
        setTransformerName("dx_dataversion");
    }

    @Override
    public Record evaluate(Record record, Object... paras) {

        try {
            if (paras.length != 2) {
                throw new RuntimeException("dx_dataversion paras must be 2");
            }
            record.getMeta();
            String dataVersion = (String) paras[1];
            Column dataVersionColumn = new StringColumn(dataVersion);
            record.addColumn(dataVersionColumn);
        } catch (Exception e) {
            throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_ILLEGAL_PARAMETER, "paras:" + Arrays.asList(paras).toString() + " => " + e.getMessage());
        }

        return record;
    }
}
